/**
 * Created by bshao on 10/16/15.
 */


import org.codehaus.jackson.JsonNode
import org.codehaus.jackson.map.ObjectMapper;


def testSer() {
    def objectMapper = new ObjectMapper(); // can reuse, share globally
    def user = new User(name: "haha", phone: "hehe")

    def writer = new StringWriter()
    def jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(writer)
    objectMapper.writeValue(jsonGenerator, user);
    if (jsonGenerator != null) {
        jsonGenerator.close();
    }

    def json = writer.toString();

    println json
}

def testDeser() {
    def text = """\
{
  "name" : { "first" : "Joe", "last" : "Sixpack" },
  "gender" : "MALE",
  "verified" : false,
  "userImage" : "Rm9vYmFyIQ=="
}
"""

    def objectMapper = new ObjectMapper(); // can reuse, share globally
    def profile = objectMapper.readValue(text, Profile.class)
    println(profile)
}

def testJsonNode() {
    def text = """\
{
  "name" : { "first" : "Joe", "last" : "Sixpack" },
  "gender" : "MALE",
  "verified" : false,
  "userImage" : "Rm9vYmFyIQ=="
}
"""

    def objectMapper = new ObjectMapper(); // can reuse, share globally
    JsonNode node = objectMapper.readTree(text)
    println node.path("gender").getTextValue()
}

def testJsonProperties() {
    def user = new User(name: "haha", phone: "123")
    def objectMapper = new ObjectMapper();
    println objectMapper.writeValueAsString(user)
}

//testSer()
//testDeser()
//testJsonNode()
testJsonProperties()


